import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/home',
    name: 'homeAlias',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },

  {
    path: '/system/user/manage',
    name: 'systemUserManage',
    component: () => import('../views/system/UserManageView.vue')
  },
  {
    path: '/system/user/add',
    name: 'addUser',
    component: () => import('../views/system/AddUserView.vue')
  },
  {
    path: '/parking/manage',
    name: 'parkingManage',
    component: () => import('../views/parking/ParkingManageView.vue')
  },
  {
    path: '/parking/add',
    name: 'addParking',
    component: () => import('../views/parking/AddParkingView.vue')
  },
  {
    path: '/card/manage',
    name: 'cardManage',
    component: () => import('../views/card/CardManageView.vue')
  },
  {
    path: '/temp/entry',
    name: 'tempEntry',
    component: () => import('../views/temp/TempEntryView.vue')
  },
  {
    path: '/fixed/manage',
    name: 'fixedManage',
    component: () => import('../views/fixed/FixedManageView.vue')
  },
  {
    path: '/payment/manage',
    name: 'paymentManage',
    component: () => import('../views/payment/PaymentManageView.vue')
  },
  {
    path: '/report/view',
    name: 'reportView',
    component: () => import('../views/report/ReportView.vue')
  },
  {
    path: '/settings/password',
    name: 'passwordSettings',
    component: () => import('../views/settings/PasswordSettingsView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 检查是否为登录页面或公共页面
  if (to.path === '/login') {
    next()
    return
  }
  
  // 检查是否登录
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
  if (!isLoggedIn) {
    next('/login')
  } else {
    next()
  }
})

export default router